package com.example.ebusinessvue.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ebusinessvue.common.http.ResponseResult;
import com.example.ebusinessvue.common.sercurity.utiles.ConfigrarionBean;
import com.example.ebusinessvue.common.sercurity.utiles.JwtTokenUtil;
import com.example.ebusinessvue.common.sercurity.utiles.RedisUtil;
import com.example.ebusinessvue.entity.AUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

//@RestController
//@RequestMapping("/api/admin")
//public class AuserServiceImpl extends ServiceImpl<AuserMapper, AUserEntity> implements AuserService {
//    @Autowired
//    private AuserService auserService;
//    public ResponseResult<Map<String , String>> login( AUserEntity aUserEntity) {
//        return null;
//    }
//}

@Service
public class AuserServiceImpl extends ServiceImpl<AuserMapper, AUserEntity> implements AuserService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ConfigrarionBean config;
    @Override
    public ResponseResult<Map<String,String>> login(AUserEntity aUserEntity) {

        //获取到这个参数
        //对传入的对象参数进行比较
        //1.有这个用户：a.密码正确；b.密码错误
        //无此用户：返回
        //2.无这个用户：返回token，跳转到相应展示页面
        Long count = lambdaQuery().eq(AUserEntity::getAname, aUserEntity.getAname()).count();
        if(count==0){
            return ResponseResult.getMessageResult(null,"A001");
        }
        HashMap<String, String> mapuser = new HashMap<>();
        mapuser.put("aname",aUserEntity.getAname());
        mapuser.put("apwd",aUserEntity.getApwd());
        List<AUserEntity> userlist = query().allEq(mapuser).list();
        if(!userlist.isEmpty()){
            String token = jwtTokenUtil.createToken(aUserEntity.getAname());
            redisUtil.set("login_" + aUserEntity.getAname(), aUserEntity.getAname(), config.getRedisExpiration());
            //把生成的有效信息返回给ResponseResult
            HashMap<String, String> loginresult = new HashMap<>();
            loginresult.put("authtoken",token);
            loginresult.put("aname",aUserEntity.getAname());
            loginresult.put("aid",aUserEntity.getId()+"");
            return ResponseResult.getSuccessResult(loginresult);
        } else {
            return ResponseResult.getMessageResult(null,"A002");
        }

    }
}
